/*测试数据*/
var usertestdata=[
	{
		id: 1,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	},
	{
		id: 2,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	}
];

$(function(){
	
	$("#userSearch").on('click',function(){
		searchUserData();
	});
	$("#userKeys").bind("keydown", function (e) {
		var key = e.which;
		if (key == 13){
			searchUserData();
		}
	});
	
	$("#btnCreateFinance").on('click',function(){
		createFinance();
	});
});
/*客户信息展示*/
function userMesShow(datarow){
    if(datarow!=null){
        if(datarow.length==undefined){
            var arr=[];
            arr.push(datarow);
            datarow=arr;
        }
        if(datarow.length>0){
            $("#userList").empty();
            $.each(datarow, function(i,item) {
                var $successtr =$("<tr>").addClass("success user-item");
                var $userrd =$("<td>").addClass("userrd");
                var $userId =$("<td>").addClass("userId").text(item.userId);
                var $realName =$("<td>").addClass("realName").text(item.realName);
                var $phone =$("<td>").addClass("phone").text(item.phone);
                var $userMoney =$("<td>").addClass("userMoney").text(item.userMoney);
                var $address =$("<td>").addClass("address").text(item.address);
                var $shoppingAddress =$("<td>").addClass("shoppingAddress").text(item.shoppingAddress);
                var $radioIndex =$("<input>").addClass("radio-user").attr({'type':'radio','name':'useritem',"shoppingAddress":item.shoppingAddress}).val(item.userId);
                $userrd.append($radioIndex);
                $successtr.append($userrd,$userId,$realName,$phone,$userMoney,$address,$shoppingAddress);

				/*展示开票信息*/
                var $infotr =$("<tr>").addClass("info user-item");
                var $taxMes =$("<td>").addClass("taxMes").attr({'colspan':7});
                var $taxh5=$("<h5>").text("客户:"+item.realName+"的开票信息：");
                var $taxType=$("<label>").addClass("label-ml").text("开票类型："+item.taxType);
                var $taxCompany=$("<label>").addClass("label-ml").text("开票公司："+item.taxCompany);
                var $taxId=$("<label>").addClass("label-ml").text("税务账号："+item.taxId);
                var $taxPhone=$("<label>").addClass("label-ml").text("开票电话："+item.taxPhone);
                var $taxAddress=$("<label>").addClass("label-ml").text("开票地址："+item.taxAddress);
                var $taxBankId=$("<label>").addClass("label-ml").text("开户行账户："+item.taxBankId);
                var $taxBankName=$("<label>").addClass("label-ml").text("开户行名称："+item.taxBankName);
                $taxMes.append($taxh5,$taxType,$taxCompany,$taxId,$taxPhone,$taxAddress,$taxBankId,$taxBankName);
                $infotr.append($taxMes);

                $("#userList").append($successtr,$infotr);
            });
        }else{
            consoleMes($("#manageContent"),3000,"warning","无相关数据！");//success、info、warning、danger
        }

    }else{
        consoleMes($("#manageContent"),3000,"warning","无相关数据，请重试！");//success、info、warning、danger
    }


};
/*搜索客户时*/
function searchUserData(){
	var userKeys= $("#userKeys").val().trim();
	if(userKeys!=null && userKeys!=""){
		/*var datarow = usertestdata;
		userMesShow(datarow);*/
        $.post(
            gobalServer+"user/query",
            {"keyword":userKeys},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.commonList;
                    userMesShow(datarow);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );

	}else{
		consoleMes($("#manageContent"),3000,"warning","请输入要搜索的关键字");
	}
};
/*收扣款*/
function createFinance(){
	var userid= $("input[name='useritem']:checked").val();
	var money=$("#money").val().trim();
	var payID=$("#payID").val().trim();
	var tradeType=$("#tradeType").val().trim();
	var payMethod=$("#payMethod").val().trim();
	var remark=$("#remark").val().trim();
	if(userid==null || userid==""){
		consoleMes($("#manageContent"),2000,"warning","请选择客户！");
		return;
	}
	if(tradeType==null || tradeType==""){
		consoleMes($("#manageContent"),2000,"warning","请选择汇款类型！");
		return;
	}
	if(money==null || money<=0){
		consoleMes($("#manageContent"),2000,"warning","请输入金额！");
		return;
	}
	if(payMethod==null || payMethod==""){
		consoleMes($("#manageContent"),2000,"warning","请选择转账类型！");
		return;
	}
	if(payID==null || payID==""){
		consoleMes($("#manageContent"),2000,"warning","请输入交易单号！");
		return;
	}
	/*consoleMes($("#manageContent"),2000,"success","操作成功！");//success、info、warning、danger
	setTimeout(function () {
		window.location.reload();
    }, 2500);*/
    $.post(
        gobalServer+"moneyRecord/insert",
        {"userId":userid,"tradeType":tradeType,"money":money,"payMethod":payMethod,"payId":payID,"remark":remark},
        function(backData){
            if(backData.statusCode==200){
                consoleMes($("#manageContent"),2000,"success","操作成功！");//success、info、warning、danger
                setTimeout(function () {
                    window.location.reload();
                }, 2500);
            }else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
    );

};
